import matplotlib.pyplot as plt
import numpy as np

def Bspline(i, x):
    y = 0
    if (x > i - 1) and (x <= i):
        y = (x - i + 1) * (x - i + 1) / 2
    elif (x > i) and (x <= i+1):
        y = (1 - 2*i - 2*i*i + (2+4*i)*x - 2*x*x) / 2
    elif (x > i+1) and (x <= i+2):
        y = (i+2-x) * (i+2-x) / 2
    return y

x = np.linspace(-2, 6, 300)
for i in range(-1,5):
    y = []
    for t in x:
        y1 = Bspline(i, t)
        y.append(y1)
    plt.plot(x, y, label='i = ' + str(i))
plt.xlabel("x")
plt.ylabel("y")
plt.title("B-splines for n = 2.")
plt.legend()
plt.savefig("./ReportFigure/FigureE.png")

print("Finish ploting.")